# -*- coding: utf-8 -*-
import re
import sys
import os
import traceback

def read_directory(path):
    for root, dirs, files in os.walk(path):
        for file in files:
            full_path = os.path.join(root, file)
            _, extension = os.path.splitext(full_path)
            if extension != '.php':
                break
            try:
                for i in ('utf-8','gbk','gb18030','ansi'): 
                    try:
                        with open(full_path, 'r', encoding=i) as fileo:
                            #chr(973 -876)
                            cont = fileo.read()
                            # res = re.findall(r'if\s\(\$[A-Za-z\_]{3,}\)\{class\s[A-Za-z0-9\_]{4,}\{public\sfunction\s[A-Za-z]{3,}\(\)\{echo\s\"\d{2,}\"\;\}', cont)
                            # res = re.findall(r'cookie-consent\{display:none', cont)
                            res = re.findall(r'ass\x65rt', cont)
                            if len(res):
                                print(f'\033[91mbiu:{full_path}\033[0m')
                            # else:
                            #     print(f'\033[92m{full_path} suc\033[0m')
                            
                            break
                    except Exception as e:
                        if i=='ansi':
                            print(f'\033[91mFAIL: {full_path} \033[0m')
                            print(e)
            except Exception as e:
                print(e)
                print(traceback.print_exc())
                print(f"     =>{full_path}")
                return
            # if len(res)>0:
                # print(cont)
            


if __name__ == '__main__':
    arg = sys.argv
    arg = arg[1] if len(arg)>1 else ''
    path = arg if arg else './'
    read_directory(path)
